package com.scuec.groupfive.controller;

import com.scuec.groupfive.entity.UserFeedback;
import com.scuec.groupfive.service.UserFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/feedback")
@CrossOrigin
public class UserFeedbackController {

    @Autowired
    private UserFeedbackService userFeedbackService;

    /**
     * 获取用户反馈列表（分页）
     * @param pageNum 当前页码
     * @param pageSize 每页数量
     * @param timeRange 时间范围（today/3days/week/overweek）
     * @param status 状态（pending/processing/resolved/rejected）
     * @param feedbackType 反馈类型（question/system/suggestion/other）
     * @param searchType 搜索类型（content/userId/username/feedbackId）
     * @param keyword 搜索关键词
     * @return 分页反馈列表
     */
    @GetMapping("/list")
    public Map<String, Object> getFeedbackList(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String timeRange,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String feedbackType,
            @RequestParam(required = false) String searchType,
            @RequestParam(required = false) String keyword) {
        return userFeedbackService.getFeedbackList(pageNum, pageSize, timeRange, status, feedbackType, searchType, keyword);
    }

    /**
     * 获取反馈总数
     * @return 反馈总数
     */
    @GetMapping("/count")
    public int getFeedbackCount() {
        return userFeedbackService.getFeedbackCount();
    }

    /**
     * 根据ID获取单个反馈详情
     * @param id 反馈ID
     * @return 反馈详情
     */
    @GetMapping("/detail/{id}")
    public UserFeedback getFeedbackDetail(@PathVariable String id) {
        return userFeedbackService.getFeedbackDetail(id);
    }

    /**
     * 删除用户反馈
     * @param id 反馈ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public String deleteFeedback(@PathVariable String id) {
        boolean result = userFeedbackService.deleteFeedback(id);
        return result ? "删除成功" : "删除失败";
    }

    /**
     * 刷新反馈列表
     * @return 刷新后的反馈列表
     */
    @GetMapping("/refresh")
    public List<UserFeedback> refreshFeedbackList() {
        return userFeedbackService.refreshFeedbackList();
    }
}